import { Component, OnInit } from '@angular/core';
import { Course } from '../../model/course';
import { Student } from '../../model/Student';
import { Book } from '../../model/book';
import { CourseResult } from '../../model/courseresult';
import { CourseService } from '../../service/course.service';
import { Classroom } from '../../model/classroom';
import { Teacher } from '../teacher';
import { Router } from '@angular/router';

@Component({
  selector: 'app-scheduling',
  templateUrl: './scheduling.component.html',
  styleUrls: ['./scheduling.component.css']
})
export class SchedulingComponent implements OnInit {
  cl:Array<Course>
  sl:Array<Student>
  bl:Array<Book>
  ml:Array<Classroom>
  rl:Array<Teacher>
  tosavecourse:CourseResult
  constructor(
    private courseservice:CourseService,
    private router:Router) { 
    this.tosavecourse = new CourseResult();
  }

  ngOnInit() {
    this.initCourse();
    this.initStudent();
    this.initBook();
    this.initClassroom();
    this.initTeacher();
  }

  initCourse(){
    this.cl = new Array();
    for(let i=0 ;i<25;i++){
      let c = new Course()
      c.id= i;
      this.cl.push(c);
    }
   }

   setCourse(searchresult:Array<CourseResult>){
    for(let x of searchresult){
      let t = new Course();
      t.classroom= x.classroom
      t.id=x.cid
      t.name= x.name
      t.student= x.student
      t.teacher= x.teacher
      this.cl[x.cid]=t;
    }
   }
 
 
 //初始化班级信息
   initStudent(){
    this.sl = new Array();
    this.courseservice.getStudentList().then((data:any)=>{
    this.sl= data;
   })
   }
 
   onStudentSelect(s:Student){
    console.dir(s);
    this.courseservice.getCourseListByParm('student',s.name).then((data:any)=>{
      console.dir(data);
      this.setCourse(data);
      })
   }
 
 
   //初始化课程信息
   initBook(){
    this.bl = new Array();
    this.courseservice.getBookList().then((data:any)=>{
    this.bl= data;
   })
  }

  onBookSelect(b:Book){
    console.dir(b);
     this.courseservice.getCourseListByParm('book',b.name).then((data:any)=>{
     console.dir(data);
    this.setCourse(data);
     })
    }

    //初始化教室信息
   initClassroom(){
    this.ml = new Array();
    this.courseservice.getClassroomList().then((data:any)=>{
    this.ml= data;
   })
  }

  onClassroomSelect(m:Classroom){
    console.dir(m);
     this.courseservice.getCourseListByParm('classroom',m.name).then((data:any)=>{
     console.dir(data);
    this.setCourse(data);
     })
    }


    //初始化教师信息
   initTeacher(){
    this.rl = new Array();
    this.courseservice.getTeacherList().then((data:any)=>{
    this.rl= data;
   })
  }

  onTeacherSelect(r:Teacher){
    console.dir(r);
     this.courseservice.getCourseListByParm('teacher',r.name).then((data:any)=>{
     console.dir(data);
    this.setCourse(data);
     })
    }
 
   //保存
   save(){
    console.dir(this.tosavecourse);
    this.courseservice.addCourse(this.tosavecourse).then((flag:boolean)=>{
      if(flag==true){
        this.goBack();
      }
      else{
        alert("添加失败，请重试");
      }
    })
   }

   //取消
   goBack(){
    this.router.navigate(['courseindex']);
  }
   
   onGridSelect(cid:number){
    alert("你选中了"+cid)
    this.tosavecourse.cid = cid
   }

   onBookValueSelect(b:Book){
    this.tosavecourse.book = b.name
    this.tosavecourse.name = b.name
   }

   onStudentValueSelect(s:Student){
    this.tosavecourse.student = s.name
    this.tosavecourse.name = s.name
   }

   onClassroomValueSelect(m:Classroom){
    this.tosavecourse.classroom = m.name
    this.tosavecourse.name = m.name
   }

   onTeacherValueSelect(r:Teacher){
    this.tosavecourse.teacher = r.name
    this.tosavecourse.name = r.name
   }



}
